package ge.telasi.tasks.ui.flowpermission;

import java.awt.Component;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import static ge.telasi.tasks.model.FlowPermission.*;

/**
 * @author dimitri
 */
public class FlowOperationCombo extends JComboBox {

    public FlowOperationCombo() {
        setModel(new DefaultComboBoxModel(new Integer[] {
            OPERATION_CHANGE_SUBJECT, OPERATION_CHANGE_PRIORITY,
            OPERATION_CHANGE_RECEIVERS, OPERATION_CHANGE_STATUS,
            OPERATION_ADD_ATTACHMENT, OPERATION_REMOVE_ATTACHMENT,
            OPERATION_DOWNLOAD_ATTACHMENT, OPERATION_DELETE_TASK,
            OPERATION_ADD_PARENT, OPERATION_REMOVE_PARENT
        }));
        setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof Integer) {
                    int oper = (Integer) value;
                    setText(FlowPermissionDecorationUtils.getOperationName(oper));
                    setIcon(FlowPermissionDecorationUtils.getOperationIcon(oper));
                }
                return this;
            }
        });
    }

    public Integer getSelection() {
        Object selection = getSelectedItem();
        if (selection instanceof Integer) {
            return (Integer) selection;
        } else {
            return null;
        }
    }

}
